function [likel,ssvec,flag_ok,lht]=likelRegSplit(parest,parvec,parposest,funcmod,Y,trainvec,solveopt,addsol)
% =========================================================================
% LikelRegSplit.m
%
% This code estimates the likelihood of a regular model, i.e. no NANs or cummulators,
% with Split Solutions, where
% all matrices (GG,RR,C,SDX,ZZ) are allowed to change in the second part of
% the sample at a timne T*
%
%% *Additional inputs needed*
% *addsol.dateBegSecond*  Numeric Calendar date when second sample begins
% (provided in settings)
% *addsol.rowBegSecond*  Position in the sample vector where second sample
% begins (computed in LMJ_initial.m)
%
% Matrices of the second sample are given in the structure second, usually
% called FLAG, in the solution of the model, e.g.
%
% [GG, RR, CONS, eu, SDX, ZZ, initss, ssvec, flag, ssnames]=feval(funcmod,param,options,addsol);
%
% =========================================================================
likel=-1e20; flag_ok=0;
parvec(parposest)=parest;
%% Model solution, second sample stored in structure second (second sample)
[G,R,C,eu,SDX,Z,structOne,ssvec,structTwo]=feval(funcmod,parvec,solveopt,addsol);
% A. Non-existence in either sample
if isequal(eu1,[1;1])==0 || second.euok==0
    return
end
[T,ny]=size(Y);
%% Demeaning using the split sample
%  First Sample (C1)
if any(C~=0)==1
    Y(1:addsol.rowBegSecond-1,:)=...
        Y(1:addsol.rowBegSecond-1,:)-repmat((Z*C)',[addsol.rowBegSecond-1 1]);
end
% Second sample (second.second.C)
if any(structOne.C~=0)==1;
    Y(addsol.rowBegSecond:end,:)=...
        Y(addsol.rowBegSecond:end,:)-repmat((structOne.Z*structOne.C)',[(T-addsol.rowBegSecond+1) 1]);
end
Y=Y';

try
    % B. Problems with initialization
    pshat=feval(@lyapunov_symm,G,(R*(SDX'))*((R*(SDX'))'));
    if ~isempty(find(isnan(pshat))~=0)
        disp('PSHAT contains Nan entries')
        return
    end
catch
    disp('Warning! Problems with initialization in GENSYSPOST_VEC');
    return;
end

shat=zeros(size(GG1,1),1);
lht=zeros(T,1);

% Filter, first part
for ii=1:addsol.rowBegSecond-1;
    [shat,pshat,lht(ii)]=feval(@kf,Y(:,ii),Z,shat,pshat,G,R*(SDX'));
end

% Filter, second part
for ii=addsol.rowBegSecond:T;
    [shat,pshat,lht(ii)]=feval(@kf,Y(:,ii),structOne.Z,shat,pshat,structOne.G,(structOne.R)*(structOne.SDX'));
end

%% Likelihood plus integration constant

lht=lht(trainvec(1):trainvec(2)); 
likel=-0.5*length(lht)*ny*log(2*pi)+sum(lht);
flag_ok=1; 

%% End of File
end